<?php
namespace app\app\controller\v2;
use app\app\Appbase;

class Goods extends Appbase 
{
	
	/**
	*获取商品列表
	* /app/v2/goods/getList
	* 需要登录 
	* @is_need_login true
	* @method get
	*/
	public function getList($keyWord='',$pageIndex=1,$pageSize=20){
		$param = request()->get();
		$where[] = [ 'shop_id', '=', request()->shopId ];
		$where[] = [ 'name', 'like', "%$keyWord%"];
		if(!empty($param['status']) || $param['status']==0){
			$where[] = ['status', '=', $param['status']];
		}
		if(!empty($param['shop_cate_id'])){
			$where[] = [ 'shop_cate_id', '=', $param['shop_cate_id']];
		}
		if($param['is_new'] == 1){
			$where[] = [ 'is_new', '=', $param['is_new']];
		}
		if($param['month'] == 1){
			$where[] = ['add_time', 'like',"%".date('Y-m')."%" ];
		}
		try {
			$result = model('Goods')->getList($where,$pageIndex,$pageSize,true);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	
	/**
	*获取商品列表
	* /app/v2/goods/details
	* 需要登录 
	* @is_need_login true
	* @method get
	*/
	public function details($id){
		if(!$id){
			return $this->ReError('商品id不能为空！');
		}
		try {
			$result = model('Goods')->details($id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	/**
	*获取商品评论列表
	* /app/v2/goods/getGoodsReplyList
	* 需要登录 
	* @is_need_login true
	* @method get
	*/
	public function getGoodsReplyList($id,$pageIndex=1,$pageSize=20){
		if(!$id){
			return $this->ReError('商品id不能为空！');
		}
		try {
			$result = model('GoodsReply')->getGoodsList($id,$pageIndex,$pageSize);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	/**
	*添加
	* /app/v2/goods_cate/add
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function add(){
		$request =  request();
		$param = $request ->post();
		$param['shop_id'] = $request ->shopInfo['id'];
		$param['add_uid'] = $request ->userId;
		try {
			$result = model('Goods')->addAndUpdate($param);
			if($result === false){
				return $this->ReError( model('Goods')->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	/**
	* 编辑
	* /app/v2/goods_cate/edit
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function edit(){
		$request =  request();
		$param = $request ->post();
		$param['shop_id'] = $request ->shopInfo['id'];
		try {
			$db = model('Goods');
			$frozen_stock = $db->where('id',$param['id'])->value('frozen_stock');
			if( $param['stock'] < $frozen_stock){
				return $this->ReError('总库存不能小于活动库存'.$frozen_stock.'!');
			}
			$result = $db->addAndUpdate($param);
			if($result === false){
				return $this->ReError( $db->errorInfo );
			}
			return $this->ReSucess();	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	
	/**
	*删除
	* /app/v2/goods_cate/del
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function del($id){
		try {
			$result = model('Goods')->del($id);
			if(!$result){
				return $this->ReError();
			}
			return $this->ReSucess();	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	/**
	*上架
	* /app/v2/goods_cate/grounding
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function grounding($id){
		if(!$id){
			return $this->ReError('商品id不能为空！');
		}
		try {
			$db = model('Goods');
			$result = $db->grounding($id);
			if(!$result){
				return $this->ReError( $db->errorInfo );
			}
			return $this->ReSucess();	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	
	/**
	*上架
	* /app/v2/goods_cate/grounding
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function undercarriage($id){
		if(!$id){
			return $this->ReError('商品id不能为空！');
		}
		try {
			$db = model('Goods');
			$result = $db->undercarriage($id);
			if(!$result){
				return $this->ReError( $db->errorInfo );
			}
			return $this->ReSucess();	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	
	/**
	* 商品审核
	* /app/v2/goods_cate/approval
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function approval($id,$status,$status_desc){
		try {
			$db = model('Goods');
			$result = $db->approval($id,$status,$status_desc);
			if(!$result){
				return $this->ReError( $db->errorInfo );
			}
			return $this->ReSucess();	
		} catch (\Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
}



